//
//  HXCollecotionFlowLayout.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/1.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXCollecotionFlowLayout.h"
#import "UIView+Frame.h"

@interface  HXCollecotionFlowLayout()
@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *>*attributesArrays;
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign) CGFloat contentHeight;
@end

@implementation HXCollecotionFlowLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArrays = [NSMutableArray array];
        _numberOfColumns = 3;
        _contentHeight = 0.0;
        _cellMargin = 5.0f;
    }
    return self;
}

- (CGFloat)itemWidth {
    return self.collectionView.HX_width / _numberOfColumns;
}
- (void)prepareLayout {
    self.contentHeight = 0.0f;
    NSInteger column = 1;
    if (_attributesArrays.count != 0) {
        [_attributesArrays removeAllObjects];
    }
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
   NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *layout = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        if (i == 0) {
        layout.frame = CGRectMake(0, 0, self.itemWidth, self.collectionView.bounds.size.height);
        } else if (i == 1 || i == 2) {
            layout.frame = CGRectMake(self.itemWidth * i, 0, self.itemWidth, [self.delegate collectionView:self.collectionView layout:self width:self.itemWidth heightForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]);
        } else {
            layout.frame = CGRectMake(self.itemWidth * column, self.collectionView.HX_height / 2, self.itemWidth, [self.delegate collectionView:self.collectionView layout:self width:self.itemWidth heightForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]);
            column++;
        }
        [_attributesArrays addObject:layout];
    }
    
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return [_attributesArrays mutableCopy];
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(100, 0);
}
@end
